import { _decorator, Camera, Component, log, Node, v3, view } from 'cc';
import { BaseComponent, cat } from '@shimotsuki/core';
const { ccclass, property } = _decorator;

@ccclass('Scene3dFitWith2d')
export class Scene3dFitWith2d extends BaseComponent {
    @property({ type: Node, tooltip: "2D参考点" })
    base2D: Node

    @property({ type: Node, tooltip: "3D参考点" })
    base3D: Node

    @property({ type: Camera, tooltip: "2D相机" })
    camera2D: Camera

    @property({ type: Camera, tooltip: "3D相机" })
    camera3D: Camera

    protected override onLoad(): void {

        // this.updateFit()
        // 通过URL获取安全区域
        const url_params = cat.util.stringUtil.getURLParameters(decodeURIComponent(window.location.href));
        let safeTop = Number(url_params.safe_top || 0);
        let safeBottom = Number(url_params.safe_bottom || 0);
        // 安全宽度
        let size = view.getVisibleSize();
        let dy = (safeBottom - safeTop) / 2 / size.height;
        let rect = this.camera3D.rect;
        rect.y = dy;
        this.camera3D.rect = rect
    }

    // protected override onEventListener(): void {
    //     cat.event.on(UPDATE_FOV, this.updateFit, this)
    // }

    // override start() {
    //     this.updateFit()
    // }

    // private updateFit() {
    //     log('updateFit')
    //     this.scheduleOnce(() => {
    //         this.camera3D.node.setWorldPosition(v3(this.camera3D.node.worldPosition.x, 0, this.camera3D.node.worldPosition.z))

    //         this.camera2D.camera.update(true)
    //         const worldPosition = this.base2D.worldPosition.clone()

    //         const worldToScreen = v3()
    //         this.camera2D.camera.worldToScreen(worldToScreen, worldPosition)

    //         const screenToWorld = v3()
    //         this.camera3D.camera.update(true)
    //         this.camera3D.camera.screenToWorld(screenToWorld, v3(worldToScreen.x, worldToScreen.y, (this.base3D.worldPosition.z - this.camera2D.near) / this.camera2D.far))

    //         const { y } = screenToWorld.clone()

    //         this.camera3D.node.setWorldPosition(v3(this.camera3D.node.worldPosition.x, y, this.camera3D.node.worldPosition.z))
    //     }, 0.016)

    // }


}


